<template>
  <!-- 清货商品搜索 -->
  <base-table-select
    v-model="storeObj"
    :disabled="disabled"
    placeholder="请输入清货商品搜索"
    class="my-form_input"
    :api-obj="storeApiObj"
    :params="params"
    :table-width="type == 'permission' ? 220 : 500"
    :props="storeProps"
    @change="change"
  >
    <template v-if="type == 'permission'">
      <el-table-column prop="name" min-width="100" label="仓库名称" align="center" />
    </template>
    <template v-else>
      <el-table-column prop="code" min-width="100" label="仓库编号" align="center" />
      <el-table-column prop="name" min-width="100" label="仓库名称" align="center" />
      <el-table-column prop="company" min-width="100" label="companyName" align="center" show-overflow-tooltip />
      <el-table-column prop="address" min-width="100" label="仓库地址" align="center" show-overflow-tooltip />
    </template>
  </base-table-select>
</template>

<script>
// 查询有数据权限仓库列表
import { reqStorageListPermission } from '@/api/module/device-management.js'
export default {
  name: 'StoreSelect',
  props: {
    type: {
      type: String,
      default: 'all' // all 所有的 permission 有权限
    },
    disabled: {
      type: Boolean,
      default: false
    },
    param: {
      type: Object,
      default: () => {
        return {}
      }
    },
    data: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      storeObj: {},
      storeApiObj: this.type == 'permission' ? reqStorageListPermission : this.reqCondition, // 库位列表请求接口
      storeProps: {
        label: 'name',
        value: 'id',
        keyword: 'name',
        page: 'page',
        pageSize: 'size'
      },
      params: {
        valid: true // 状态
      }
    }
  },
  watch: {
    param: {
      handler() {
        Object.assign(this.params, this.param)
      },
      deep: true,
      immediate: true
    },
    data: {
      handler(val) {
        this.storeObj = val.id ? Object.assign({}, this.storeObj, val) : {}
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    async reqCondition(params) {
      return new Promise(resolve => {
        this.$http.get('/ly-wms-servicer/storage/page/condition', params, res => {
          if (res && res.code != 0) {
            this.$message.error(res.message || '查询错误')
          }
          res.responseBody.records = Array.isArray(res.responseBody.records) ? res.responseBody.records : []
          res.responseBody.total = res.responseBody.total ? res.responseBody.total - 0 : 0
          resolve(res)
        })
      })
    },
    change() {
      this.$emit('change', this.storeObj)
    },
    clear() {
      this.storeObj = {}
    }
  }
}
</script>

<style lang="scss" scoped></style>
